package xin.marcher.module.replenished.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Repository;
import xin.marcher.framework.common.util.data.DataCuttingUtil;
import xin.marcher.module.common.constants.replenished.AvailableConstants;
import xin.marcher.module.common.constants.replenished.RecoverableConstants;
import xin.marcher.module.common.enums.ProductTypeEnum;
import xin.marcher.module.common.enums.seller.SellerTypeEnum;
import xin.marcher.module.replenished.domain.entity.AttributeExtendDO;
import xin.marcher.module.replenished.domain.entity.ItemInfoDO;
import xin.marcher.module.replenished.domain.entity.ProductDetailDO;
import xin.marcher.module.replenished.domain.entity.SkuInfoDO;
import xin.marcher.module.replenished.mapper.AttributeExtendMapper;
import xin.marcher.module.replenished.mapper.ItemInfoMapper;
import xin.marcher.module.replenished.mapper.SkuInfoMapper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 商品 主体表相关资源类
 *
 *
 */
@Repository
public class ProductRepository {
    @Resource
    private ItemInfoMapper itemInfoMapper;
    @Resource
    private SkuInfoMapper skuInfoMapper;
    @Resource
    private AttributeExtendMapper attributeExtendMapper;

    /**
     * 根据skuId集合批量查询商品明细
     *
     * @param skuIds
     * @return
     */
    public List<SkuInfoDO> querySkuListByIds(List<String> skuIds) {
        LambdaQueryWrapper<SkuInfoDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(SkuInfoDO::getSkuId, skuIds);
        queryWrapper.eq(SkuInfoDO::getSellerType, SellerTypeEnum.SELF.getCode());
        return skuInfoMapper.selectList(queryWrapper);
    }

    /**
     * 根据itemId集合查询item明细
     *
     * @param itemList
     * @return
     */
    public List<ItemInfoDO> queryItemList(List<String> itemList) {
        LambdaQueryWrapper<ItemInfoDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(ItemInfoDO::getItemId, itemList);
        return itemInfoMapper.selectList(queryWrapper);
    }

    /**
     * 分页查询sku信息
     *
     * @param skuIdList
     * @return
     */
    public List<ProductDetailDO> pageQueryProductInfoList(Set<String> skuIdList) {
        // 默认集合初始大小为50000，避免扩容次数频繁，一般商品的数量最多也就是几万-几十万
        List<ProductDetailDO> productDetailAllList = new ArrayList<>(RecoverableConstants.SKU_INIT_NUM);
        // 一次最大查询1000 skuId，多个分页查询,这里做数据切割
        List<Set<String>> splitList = DataCuttingUtil.dataCuttingString(skuIdList, RecoverableConstants.SKU_LIMIT_NUM);
        for (Set<String> skuIds : splitList) {
            List<ProductDetailDO> productDetailDOList = queryProductInfoList(skuIds);
            if (!CollectionUtils.isEmpty(productDetailDOList)) {
                productDetailAllList.addAll(productDetailDOList);
            }
        }
        return productDetailAllList;
    }

    /**
     * 批量查询商品的详情信息
     *
     * @param skuId
     * @return
     */
    public List<ProductDetailDO> queryProductInfoList(Set<String> skuId) {
        return skuInfoMapper.queryProductInfoList(skuId);
    }

    /**
     * 分页批量查询商品的属性信息
     *
     * @param skuIdList
     * @return
     */
    public List<AttributeExtendDO> pageQueryProductAttributeList(Set<String> skuIdList) {
        List<Set<String>> splitList = DataCuttingUtil.dataCuttingString(skuIdList, AvailableConstants.SKU_ID_LIMIT_NUM);
        List<AttributeExtendDO> productAttributeAllList = new ArrayList<>(skuIdList.size());
        for (Set<String> skuIds : splitList) {
            List<AttributeExtendDO> attributeExtendDOS = queryProductAttributeList(skuIds);
            if (!CollectionUtils.isEmpty(attributeExtendDOS)) {
                productAttributeAllList.addAll(attributeExtendDOS);
            }
        }

        return productAttributeAllList;
    }

    /**
     * 批量查询商品的属性信息
     *
     * @param skuId
     * @return
     */
    public List<AttributeExtendDO> queryProductAttributeList(Set<String> skuId) {
        LambdaQueryWrapper<AttributeExtendDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AttributeExtendDO::getParticipateType, ProductTypeEnum.SKU.getCode())
                .in(AttributeExtendDO::getParticipateId, skuId);
        return attributeExtendMapper.selectList(queryWrapper);
    }
}
